package com.kikt.rxjavaandretrofit

import android.app.Application
import android.util.Log
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory

/**
 * Created by cjl on 2017/7/21.
 */
class MyApp : Application() {

    companion object {
        val gson: Gson = GsonBuilder()
                .setPrettyPrinting()
                .create()


        val okhttpClient:OkHttpClient = OkHttpClient.Builder()
                .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build()

        val retrofit: Retrofit by lazy {
            Retrofit.Builder()
                    .client(okhttpClient)
                    .baseUrl("http://gank.io/api/")
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build()
        }
    }

    override fun onCreate() {
        super.onCreate()
    }

}

fun Any.logInfo(log: String) {
    Log.i(this.javaClass.simpleName, log)
}


fun Any.logInfo(log: Any?) {
    if (log == null) {
        logInfo("null")
        return
    }
    Log.i(this.javaClass.simpleName, log.toString())
}